package com.yanqu.road.entity.activity.shuihuyezipai.config;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.utils.probability.ProbabilityItems;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 水浒叶子牌配置
 * @Author cwq
 * @Data 2021/12/6 17:27
 */
public class ShuiHuYeZiCardActivityConfig {

    /**
     * 叶子牌斗牌积分道具ID
     */
    public int SHUIHUYEZICARD_SCORE_ID;
    /**
     * 叶子牌斗牌请帖道具ID
     */
    public int SHUIHUYEZICARD_INVITATION_ID;
    /**
     * 叶子牌金叶子道具ID
     */
    public int SHUIHUYEZICARD_GOLD_LEAF_ID;
    /**
     * 叶子牌送财道具ID
     */
    public int SHUIHUYEZICARD_GIVE_MONEY_ID;
    /**
     * 叶子牌发牌最大点数差
     */
    public int SHUIHUYEZICARD_MAX_POINTS_DIFF;
    /**
     * 叶子牌可取消匹配的最长时长毫秒
     */
    public int SHUIHUYEZICARD_MAX_WAIT_TIME;
    /**
     * 叶子牌匹配预匹配时长、扩大时长秒、取消匹配时长
     */
    public String SHUIHUYEZICARD_MATCH_PARAM;

    public List<Integer> matchParamList = new ArrayList<>();

    /**
     * 叶子牌掌柜斗牌的开启时长分
     */
    public String SHUIHUYEZICARD_PVP_OPEN_PARAM;

    /**
     * 叶子牌抽卡消耗数量：单次和五连
     */
    public String SHUIHUYEZICARD_GET_CARD_PARAM;

    /**
     * 叶子牌抽卡消耗数量 单次
     */
    public int oneConsume;

    /**
     * 叶子牌抽卡消耗数量 五连
     */
    public int fiveConsume;

    /**
     * 叶子牌抽卡满级卡转化积分千分比
     */
    public int SHUIHUYEZICARD_CONVERT_VALUE;
    /**
     * 叶子牌抽卡金卡转化普通卡数
     */
    public int SHUIHUYEZICARD_GOLD_CARD_NUM;
    /**
     * 叶子牌连胜分加成：min(当前连胜,系数1)*系数2/1000，向下取整
     */
    public String SHUIHUYEZICARD_WINNING_STREAK_PLUS_COEFFICIENT;

    public List<Integer> winStreakCoefficientList;

    /**
     * 叶子牌手牌分加成：剩余手牌*系数1/1000，向下取整
     */
    public int SHUIHUYEZICARD_REMAIN_CARD_PLUS_COEFFICIENT;
    /**
     *  叶子牌玩家模型TCD参数
     */
    public String SHUIHUYEZICARD_TCD_PARAM;
    /**
     * 叶子牌三种AI模型TCD参数
     */
    public String SHUIHUYEZICARD_AI_TCD_PARAM;
    /**
     * 叶子牌AI算法其他参数x1、x2、x3
     */
    public String SHUIHUYEZICARD_AI_PARAM;

    /**
     * 最少的匹配人数
     */
    public int SHUIHUYEZICARD_MIN_MATCH_NUM;
    /**
     *  pvp每回合时间(s)
     */
    public int SHUIHUYEZICARD_ROUND_TIME;
    /**
     *  进入匹配超过n秒后，玩家可以匹配到同区服的玩家，玩家可以匹配到非相同模式的玩家
     */
    public int SHUIHUYEZICARD_EXCEED_TIME_EXPAND_SCOPE;
    /**
     * 斗牌记录显示条数
     */
    public int SHUIHUYEZICARD_BATTLE_RECORD_SHOW_NUM;

    /**
     * 叶子牌送福银两区间(秒)
     */
    public String SHUIHUYEZICARD_GIVE_MONEY_SECONDS_NUM;

    public List<Integer> giveMoneySecondsList = new ArrayList<>();

    /**
     * 叶子牌送福后的红包获得
     */
    public String SHUIHUYEZICARD_GIVE_MONEY_REWARDS;

    public String SHUIHUYEZICARD_ANIMATION_TIME;

    /**
     * 叶子牌1.开局动画 10000ms  2.斗牌比拼 3000ms  3.事件替换 3000ms
     */
    public List<Integer> animationList = new ArrayList<>();

    public List<Integer> tcdList = new ArrayList<>();

    public List<List<Integer>> tcdParamList = new ArrayList<>();

    public List<Integer> aiList = new ArrayList<>();


    /**
     * pvp 开启区间
     */
    public List<Integer[]> openTimeList = new ArrayList<>();

    /**
     * 卡牌配置
     */
    private Map<Integer, ShuiHuYeZiCardCardConfig> cardConfigMap = new ConcurrentHashMap<>();

    /**
     * 金卡权重
     */
    private ProbabilityItems<ShuiHuYeZiCardCardConfig> goldProbabilityItems;

    /**
     * buff配置
     */
    private Map<Integer, ShuiHuYeZiCardBuffConfig> buffConfigMap = new ConcurrentHashMap<>();

    /**
     * 每个回合可以随机buff的集合 k:回合数，k:buffId v:权重
     */
    private Map<Integer, Map<Integer,Integer>>  roundBuffIdMap = new HashMap<>();

    /**
     * 段位配置
     */
    private Map<Integer, ShuiHuYeZiCardLevelConfig> levelConfigMap = new ConcurrentHashMap<>();

    /**
     * 段位配置
     */
    private List<ShuiHuYeZiCardLevelConfig> levelConfigList = new ArrayList<>();

    /**
     * 房间配置
     */
    private Map<Integer, ShuiHuYeZiCardRoomConfig> roomConfigMap = new ConcurrentHashMap<>();


    public Map<Integer, ShuiHuYeZiCardCardConfig> getCardConfigMap() {
        return cardConfigMap;
    }

    public void setCardConfigMap(Map<Integer, ShuiHuYeZiCardCardConfig> cardConfigMap) {
        this.cardConfigMap = cardConfigMap;
    }

    public Map<Integer, ShuiHuYeZiCardBuffConfig> getBuffConfigMap() {
        return buffConfigMap;
    }

    public void setBuffConfigMap(Map<Integer, ShuiHuYeZiCardBuffConfig> buffConfigMap) {
        this.buffConfigMap = buffConfigMap;
    }

    public Map<Integer, ShuiHuYeZiCardLevelConfig> getLevelConfigMap() {
        return levelConfigMap;
    }

    public void setLevelConfigMap(Map<Integer, ShuiHuYeZiCardLevelConfig> levelConfigMap) {
        this.levelConfigMap = levelConfigMap;
    }

    public List<ShuiHuYeZiCardLevelConfig> getLevelConfigList() {
        return levelConfigList;
    }

    public void setLevelConfigList(List<ShuiHuYeZiCardLevelConfig> levelConfigList) {
        this.levelConfigList = levelConfigList;
    }

    public Map<Integer, ShuiHuYeZiCardRoomConfig> getRoomConfigMap() {
        return roomConfigMap;
    }

    public void setRoomConfigMap(Map<Integer, ShuiHuYeZiCardRoomConfig> roomConfigMap) {
        this.roomConfigMap = roomConfigMap;
    }

    public Map<Integer, Map<Integer,Integer>> getRoundBuffIdMap() {
        return roundBuffIdMap;
    }


    public ProbabilityItems<ShuiHuYeZiCardCardConfig> getGoldProbabilityItems() {
        return goldProbabilityItems;
    }

    /**
     * 初始化活动配置
     * @return
     */
    public static ShuiHuYeZiCardActivityConfig parseConfig(ShuiHuYeZiCardActivityConfig activityConfig, Map<String, ActivityConfig> activityConfigMap, ActivityInfo activityInfo){
        activityConfig.SHUIHUYEZICARD_SCORE_ID = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_SCORE_ID.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_INVITATION_ID = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_INVITATION_ID.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_GOLD_LEAF_ID = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_GOLD_LEAF_ID.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_GIVE_MONEY_ID = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_GIVE_MONEY_ID.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_MAX_POINTS_DIFF = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_MAX_POINTS_DIFF.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_MATCH_PARAM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_MATCH_PARAM.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_PVP_OPEN_PARAM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_PVP_OPEN_PARAM.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_GET_CARD_PARAM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_GET_CARD_PARAM.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_CONVERT_VALUE = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_CONVERT_VALUE.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_GOLD_CARD_NUM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_GOLD_CARD_NUM.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_WINNING_STREAK_PLUS_COEFFICIENT = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_WINNING_STREAK_PLUS_COEFFICIENT.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_REMAIN_CARD_PLUS_COEFFICIENT = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_REMAIN_CARD_PLUS_COEFFICIENT.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_TCD_PARAM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_TCD_PARAM.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_AI_TCD_PARAM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_AI_TCD_PARAM.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_AI_PARAM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_AI_PARAM.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_MIN_MATCH_NUM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_MIN_MATCH_NUM.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_ROUND_TIME = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_ROUND_TIME.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_EXCEED_TIME_EXPAND_SCOPE = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_EXCEED_TIME_EXPAND_SCOPE.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_BATTLE_RECORD_SHOW_NUM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_BATTLE_RECORD_SHOW_NUM.getName()).getIntValue();
        activityConfig.SHUIHUYEZICARD_GIVE_MONEY_SECONDS_NUM = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_GIVE_MONEY_SECONDS_NUM.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_GIVE_MONEY_REWARDS = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_GIVE_MONEY_REWARDS.getName()).getValue();
        activityConfig.SHUIHUYEZICARD_ANIMATION_TIME = activityConfigMap.get(eActivityConfigType.SHUIHUYEZICARD_ANIMATION_TIME.getName()).getValue();

        // 解析配置
        String[] consume = activityConfig.SHUIHUYEZICARD_GET_CARD_PARAM.split(";");
        activityConfig.oneConsume = Integer.valueOf(consume[0]);
        activityConfig.fiveConsume = Integer.valueOf(consume[1]);

        String[] pvpOpenTime = activityConfig.SHUIHUYEZICARD_PVP_OPEN_PARAM.split("\\|");
        for (String openStr : pvpOpenTime){
            String[] openArray = openStr.split(";");
            Integer[] openEnd = new Integer[2];
            openEnd[0] = Integer.valueOf(openArray[0]);
            openEnd[1] = Integer.valueOf(openArray[1]);
            activityConfig.openTimeList.add(openEnd);
        }

        activityConfig.tcdList = StringUtils.stringToIntegerList(activityConfig.SHUIHUYEZICARD_TCD_PARAM,";");
        activityConfig.animationList = StringUtils.stringToIntegerList(activityConfig.SHUIHUYEZICARD_ANIMATION_TIME,";");
        activityConfig.winStreakCoefficientList = StringUtils.stringToIntegerList(activityConfig.SHUIHUYEZICARD_WINNING_STREAK_PLUS_COEFFICIENT,";");

        activityConfig.matchParamList = StringUtils.stringToIntegerList(activityConfig.SHUIHUYEZICARD_MATCH_PARAM,";");

        List<String> tempTcdParamList =  StringUtils.stringToStringList(activityConfig.SHUIHUYEZICARD_AI_TCD_PARAM,"\\|");
        for (int i = 0; i < tempTcdParamList.size(); i++) {
            activityConfig.tcdParamList.add(StringUtils.stringToIntegerList(tempTcdParamList.get(i),";"));
        }
        activityConfig.aiList = StringUtils.stringToIntegerList(activityConfig.SHUIHUYEZICARD_AI_PARAM,";");
        activityConfig.giveMoneySecondsList = StringUtils.stringToIntegerList(activityConfig.SHUIHUYEZICARD_GIVE_MONEY_SECONDS_NUM,";");

        // 解析buff
        Map<Integer, Map<Integer,Integer>>  tempRoundBuffMap = new HashMap<>();
        for (ShuiHuYeZiCardBuffConfig item : activityConfig.getBuffConfigMap().values()){
            for (int i = item.conditionList.get(0); i <= item.conditionList.get(1); i++) {
                Map<Integer,Integer> map = tempRoundBuffMap.get(i);
                if(map == null){
                    map = new HashMap<>();
                    tempRoundBuffMap.put(i,map);
                }
                map.put(item.getId(),item.getWeight());
            }
        }
        activityConfig.roundBuffIdMap = tempRoundBuffMap;

        activityConfig.levelConfigList.addAll(activityConfig.getLevelConfigMap().values());
        activityConfig.levelConfigList.sort((t1,t2)->{
            return Integer.compare(t1.getId(),t2.getId());
        });
        activityConfig.goldProbabilityItems = new ProbabilityItems<>(new ArrayList<>(activityConfig.getCardConfigMap().values()),(e)->e.getGoldWeight());

        return activityConfig;
    }

}
